package com.masterclient.app.businessmodule.minemodule.message.adapter

import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.eoner.common.config.Config
import com.masterclient.app.R
import com.masterclient.app.businessmodule.usermodule.login.index.QuickLoginActivity
import com.masterclient.app.businessmodule.minemodule.message.detail.ActivityListActivity
import com.masterclient.app.businessmodule.minemodule.message.CouponMsgActivity
import com.eoner.baselibrary.bean.personal.MessageCenterBean
import com.eoner.baselibrary.utils.GlideUtil
import com.eoner.baselib.utils.sp.SPUtils
import com.eoner.managerlibrary.sobot.SobotManager
import com.eoner.managerlibrary.user.UserDataManager
import com.masterclient.app.businessmodule.minemodule.message.OtherMsgActivity
import com.sobot.chat.SobotApi
import com.sobot.chat.api.enumtype.SobotChatTitleDisplayMode

/**
 * 作者：sean
 * 创建时间：2018/5/22 17:08
 * 功能说明：
 */
class MsgCenterAdapter:BaseQuickAdapter<MessageCenterBean.MsgItem,BaseViewHolder>(R.layout.item_msg_center) {

    val spUtil = SPUtils.getInstance()


    override fun convert(helper: BaseViewHolder, item: MessageCenterBean.MsgItem) {

        GlideUtil.displayImage(mContext,item.sh_image,helper.getView<ImageView>(R.id.iv_customer))
//        if(!TextUtils.isEmpty(item.sh_image)){
//           App.picasso.load(item.sh_image).into(helper.getView<ImageView>(R.id.iv_customer))
//        }

        helper.setText(R.id.tv_title,item.sh_title)
        helper.setText(R.id.tv_sub_title,item.sh_sub_title)

        if("1" == item.sh_id){
            helper.getView<View>(R.id.tv_new_text).visibility = View.GONE
        }
        helper.getView<LinearLayout>(R.id.ll_con).setOnClickListener {
            if("1" == item.sh_id){
                if(TextUtils.isEmpty(SPUtils.getInstance().getString(Config.CUSTOMER_TOKEN))){
                    val intent = Intent(mContext, QuickLoginActivity::class.java)
                    intent.putExtra("intentSource","消息中心点击客服")
                    mContext.startActivity(intent)
                }else{
                    val info = SobotManager.getBaseInfo()
                    //SobotApi.setChatTitleDisplayMode(mContext, SobotChatTitleDisplayMode.ShowFixedText, "消息中心")
                    SobotApi.setChatTitleDisplayMode(mContext, SobotChatTitleDisplayMode.Default, "")
                    SobotApi.startSobotChat(mContext, info)
                }
//                CustomerService.getInstance(mContext).connectService("中华大师汇", null, Config.CUSTOMER)
            }else if("2" == item.sh_id){
                mContext.startActivity(Intent(mContext, ActivityListActivity::class.java))
            } else if("4" == item.sh_id){
                mContext.startActivity(Intent(mContext,CouponMsgActivity::class.java))
            } else if("5" == item.sh_id){
//                mContext.startActivity(Intent(mContext,TeamMsgActivity::class.java))
            } else if("6" == item.sh_id){
                if(!UserDataManager.getInstance().isSystemLogin){
                    val intent = Intent(mContext, QuickLoginActivity::class.java)
                    intent.putExtra("intentSource","消息中心点击其他消息")
                    mContext.startActivity(intent)
                }else {
                    mContext.startActivity(Intent(mContext, OtherMsgActivity::class.java))
                }
            }
        }
            when {
                "2" == item.sh_id -> {
                    if(TextUtils.isEmpty(spUtil.getString(Config.ACTIVITY_MSG))){
                        if("0"!=item.sh_latest_id){
                            helper.getView<View>(R.id.tv_new_text).visibility = View.VISIBLE
                        }else{
                            helper.getView<View>(R.id.tv_new_text).visibility = View.GONE
                        }
                    }else{
                        if(item.sh_latest_id.toInt()>spUtil.getString(Config.ACTIVITY_MSG).toInt()){
                            spUtil.put(Config.ACTIVITY_MSG,item.sh_latest_id)
                            helper.getView<View>(R.id.tv_new_text).visibility = View.VISIBLE
                        }else{
                            helper.getView<View>(R.id.tv_new_text).visibility = View.GONE
                        }
                    }
                }
                "3" == item.sh_id -> {
                    if(TextUtils.isEmpty(spUtil.getString(Config.NORMAL_MSG))){
                        if("0"!=item.sh_latest_id){
                            helper.getView<View>(R.id.tv_new_text).visibility = View.VISIBLE
                        }else{
                            helper.getView<View>(R.id.tv_new_text).visibility = View.GONE
                        }
                    }else{
                        if(item.sh_latest_id.toInt()>spUtil.getString(Config.NORMAL_MSG).toInt()){
                            spUtil.put(Config.NORMAL_MSG,item.sh_latest_id)
                            helper.getView<View>(R.id.tv_new_text).visibility = View.VISIBLE
                        }else{
                            helper.getView<View>(R.id.tv_new_text).visibility = View.GONE
                        }
                    }
                }
                "4" == item.sh_id -> {
                    if(TextUtils.isEmpty(spUtil.getString(Config.COUPON_MSG))){
                        if("0"!=item.sh_latest_id){
                            helper.getView<View>(R.id.tv_new_text).visibility = View.VISIBLE
                        }else{
                            helper.getView<View>(R.id.tv_new_text).visibility = View.GONE
                        }
                    }else{
                        if(item.sh_latest_id.toInt()>spUtil.getString(Config.COUPON_MSG).toInt()){
                            spUtil.put(Config.COUPON_MSG,item.sh_latest_id)
                            helper.getView<View>(R.id.tv_new_text).visibility = View.VISIBLE
                        }else{
                            helper.getView<View>(R.id.tv_new_text).visibility = View.GONE
                        }
                    }
//                    spUtil.put(Config.COUPON_MSG,item.sh_latest_id)
                }
                else -> {
                    if(item.sh_latest_id.toInt() >0){
                        helper.getView<View>(R.id.tv_new_text).visibility = View.VISIBLE
                    }else{
                        helper.getView<View>(R.id.tv_new_text).visibility = View.GONE
                    }
                }
            }

    }
}